package com.android.phoenix.activity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;

import com.android.phoenix.MainApplication;
import com.android.phoenix.R;
import com.android.phoenix.activity.location.LocationUtils;
import com.android.phoenix.adapters.MyExpandableListAdapter;
import com.android.phoenix.models.SportRecord;

/**
 * @ClassName: HealthActivity
 * @Description: 显示运动记录
 * @author: wanchao
 * @date: 2015-1-18 上午1:25:04
 */
public class HealthActivity extends ExpandableListActivity {

	private List<String> dateGroup = null;

	private List<List<Map<String, String>>> childs = null;

	MyExpandableListAdapter mExpandableListAdapter = null;

	List<SportRecord> listSportRecord = new ArrayList<SportRecord>();

	ExpandableListView listView = null;

	/**
	 * @Title: loadDatas
	 * @Description: 加载数据源
	 * @author: WanChao
	 * @date: 2015-1-18 下午2:07:12
	 */
	private void loadDatas() {
		try {
			listSportRecord = MainApplication.getDataHelper(this)
					.getSportRecordDao().queryForAll();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		dateGroup = new ArrayList<String>();
		childs = new ArrayList<List<Map<String, String>>>();

		String date = "----/--/--";
		List<Map<String, String>> listMap = new ArrayList<Map<String, String>>();
		for (int i = 0; i < listSportRecord.size(); i++) {
			SportRecord temp = listSportRecord.get(i);
			Map<String, String> map = new HashMap<String, String>();
			map.put("ID", temp.getId());
			String time = LocationUtils.countTime(
					Long.parseLong(temp.getStartTime()),
					Long.parseLong(temp.getEndTime()));
			map.put("time", time);
			map.put("distance", temp.getDistance());
			map.put("calorie", temp.getCalorie());

			if (date.equals(temp.getDate())) {
				listMap.add(map);
			} else {
				date = temp.getDate();
				dateGroup.add(date);
				if (listMap.size() > 0) {
					childs.add(listMap);
					listMap = new ArrayList<Map<String, String>>();
				}
				listMap.add(map);
			}
		}
		if (null != listMap && listMap.size() > 0) {
			childs.add(listMap);
			listMap = new ArrayList<Map<String, String>>();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_health);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		loadDatas();
		mExpandableListAdapter = new MyExpandableListAdapter(this, dateGroup,
				childs);
		setListAdapter(mExpandableListAdapter);
	}

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub

		Toast.makeText(this, "点击了item", Toast.LENGTH_SHORT).show();
		Intent it = new Intent(this, SportRecordDetailActivity.class);
		it.putExtra("ID", childs.get(groupPosition).get(childPosition)
				.get("ID"));
		startActivity(it);
		overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
		return false;
	}
}
